/**
 * zmn文章模块-文章编辑
 * 文件命名格式：zmn.模块.表/功能.jas
 * @author heguanghua
 * @since 2018/10/18
 */
layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'zmnSelect'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;
    var layedit = layui.layedit;
    let zmnSelect = layui.zmnSelect;
    // let formSelects = layui.formSelects;
    // 維修項目 com.zmn.plat.common.constant.ItemCategoryConsts#MAINTAIN_ITEM
    const MAINTAIN_ITEM = '3001';

    labelSelect.init('acceptProjectList', null, "/service/item/listAcceptProject",null,null,1,json.acceptProjectList);
    labelSelect.init('serviceContentList', null, "/service/item/listServiceContent",null,null,1,json.serviceContentList);
    labelSelect.init('faultList', null, "/service/item/listFault",null,null,1,json.faultList);

    // zmnSelect.selectBox("acceptProjectList", {
    //         propMap: {  //选项参数映射配置
    //             name: "text",   //显示值
    //             value: "value"  //选中值
    //         },
    //         remote: {
    //             enable: true,
    //             url: '/service/item/listAcceptProject',
    //             events: {
    //                 /**
    //                  * 请求成功回调
    //                  * @param result 响应结果
    //                  * @param callback xmSelect回调函数
    //                  */
    //                 success: function (result, callback) {
    //                     console.log(result);
    //                     console.log(result.data.data.list)
    //                 },
    //             }
    //         }
    //     }
    // )


    zmnLayUtil.layFormVerify(form);
    // 自定义验证规则
    form.verify({
        percent: function(value){
            if(value){
                if (!/^[0-9]+$/.test(value)) {
                    return "配件包干费只能是正整数";
                }
                if(value.length > 3||Number(value)<0||Number(value)>100){
                    return "配件包干费只能是0-100的正整数";
                }
            }

        },
        feeVerify: function(value){
            if(value){
                if (!/^[0-9]+$/.test(value)) {
                    return "配件包干费只能是正整数";
                }
                if(value.length > 3||Number(value)<0||Number(value)>105){
                    return "只能填入0-105的正整数";
                }
            }

        },
        maxDay:function(value){
            if(value){
                if (!/^[0-9]+$/.test(value)) {
                    return "保修天数必须是正整数";
                }
                if(value.length > 5||Number(value)<0||Number(value)>10000){
                    return "保修天数最大10000";
                }
            }

        },
        sort: function(value){
            if (!/^[0-9]+$/.test(value)) {
                return "排序值只能是正整数";
            }
        },
        serviceMinutes: function(value){
            if (value&&value!=''&&!/^0$|^[1-9]\d*$/.test(value)) {
                return "服务时长只能是正整数";
            }
        }
    });
    //一口价联动问题
    doRenderPriceDiv(json.item.priceStandard,json.item.priceSplit,json.faultTariff.tariffType,true);

    // 提交
    form.on('submit(zmn-form-submit)', function(obj){
        let data = obj.field;
        //必填项检查
        //一口价
        if(!data.priceStandard||data.priceStandard==''){
            parent.layui.layer.msg("是否一口价必选",{"icon":2});
            return false;
        }
        //一口价是否区分明细必填
        if((!data.priceSplit||data.priceSplit=='')&&data.priceStandard==2){
            parent.layui.layer.msg("是否区分明细必选",{"icon":2});
            return false;
        }
        //一口价，区分明细的情况下，配件包干费必填
        if(data.priceStandard==2&&data.priceSplit==2&&(json.faultTariff.tariffType==2)){
            let priceSplitType = data.priceSplitType;
            if(!priceSplitType){
                parent.layui.layer.msg("明细类型不能为空",{"icon":2});
                return false;
            }
            if(priceSplitType==1){
                if(!data.partRatio||data.partRatio==''){
                    parent.layui.layer.msg("配件包干费不能为空",{"icon":2});
                    return false;
                }
                if(!data.artRatio||data.artRatio==''){
                    parent.layui.layer.msg("工艺费不能为空",{"icon":2});
                    return false;
                }
                if(!data.guaranteeRatio||data.guaranteeRatio==''){
                    parent.layui.layer.msg("保修费不能为空",{"icon":2});
                    return false;
                }
                if(data.artRatio*1+data.partRatio*1+data.guaranteeRatio*1>=100){
                    parent.layui.layer.msg("配件包干费、工艺费、保修费之和必须小于100",{"icon":2});
                    return false;
                }

                if(!data.hourFee||data.hourFee==''){
                    parent.layui.layer.msg("工时费不能为空",{"icon":2});
                    return false;
                }
                if(!data.trafficFee||data.trafficFee==''){
                    parent.layui.layer.msg("交通费不能为空",{"icon":2});
                    return false;
                }
                if(!data.checkFee||data.checkFee==''){
                    parent.layui.layer.msg("检测费不能为空",{"icon":2});
                    return false;
                }
                if(data.hourFee*1+data.trafficFee*1+data.checkFee*1>105){
                    parent.layui.layer.msg("交通费、工时费、检测费之和必须不超过105",{"icon":2});
                    return false;
                }
            }else if (priceSplitType==2){
                let materialFee = data.materialFee;
                let numberStep = data.numberStep;
                let numberStepDown = data.numberStepDown;
                let baseDiscount = data.baseDiscount;
                if(!materialFee||materialFee==''){
                    parent.layui.layer.msg("材料费不能为空",{"icon":2});
                    return false;
                }
                if(!numberStep||numberStep==''){
                    parent.layui.layer.msg("数量阶梯值不能为空",{"icon":2});
                    return false;
                }
                if(!numberStepDown||numberStepDown==''){
                    parent.layui.layer.msg("阶梯降幅不能为空",{"icon":2});
                    return false;
                }

                if(!baseDiscount||baseDiscount==''){
                    parent.layui.layer.msg("底折不能为空",{"icon":2});
                    return false;
                }
                if(baseDiscount<50||baseDiscount>99){
                    parent.layui.layer.msg("底折必须大于等于50小于等于99",{"icon":2});
                    return false;
                }
                let tempdis = 100-baseDiscount;
                if(numberStepDown<1||numberStepDown>tempdis){
                    parent.layui.layer.msg("阶梯降幅必须大于等于1且小于等于100-底折",{"icon":2});
                    return false;
                }

            }

        }
        if(data.priceStandard==2&&data.priceSplit==2&&json.faultTariff.tariffType==4 && $("#categId").val() == MAINTAIN_ITEM){
            if (!data.acceptProjectList || data.acceptProjectList==''){
                parent.layui.layer.msg("请选择验收项目",{"icon":2});
                return false;
            }
            if (!data.serviceContentList || data.serviceContentList==''){
                parent.layui.layer.msg("请选择服务内容",{"icon":2});
                return false;
            }
            if (!data.faultList || data.faultList==''){
                parent.layui.layer.msg("请选择故障",{"icon":2});
                return false;
            }
            // if (!data.maintainDifficulty || data.maintainDifficulty==''){
            //     parent.layui.layer.msg("请选择维修难度",{"icon":2});
            //     return false;
            // }
        }
        //状态
        if(!data.status||data.status==''){
            parent.layui.layer.msg("状态必选",{"icon":2});
            return false;
        }
        //默认项目
        if(!data.general||data.general==''){
            parent.layui.layer.msg("默认项目必选",{"icon":2});
            return false;
        }
        //互斥性
        if(!data.mutexMode||data.mutexMode==''){
            parent.layui.layer.msg("互斥性必选",{"icon":2});
            return false;
        }
        //城市系数影响
        if(!data.cityLimit||data.cityLimit==''){
            parent.layui.layer.msg("城市系数必选",{"icon":2});
            return false;
        }
        //品牌系数影响
        if(!data.brandLimit||data.brandLimit==''){
            parent.layui.layer.msg("品牌系数必选",{"icon":2});
            return false;
        }
        if(!data.editEnable||data.editEnable==''){
            parent.layui.layer.msg("APP编辑必选",{"icon":2});
            return false;
        }
        if(!data.show||data.show==''){
            parent.layui.layer.msg("APP显示必选",{"icon":2});
            return false;
        }
        if(!data.showPrice||data.showPrice==''){
            parent.layui.layer.msg("前端价格显示必选",{"icon":2});
            return false;
        }

        //工程师拍照
        if(!data.takePicture||data.takePicture==''){
            parent.layui.layer.msg("工程师拍照必选",{"icon":2});
            return false;
        }

        //设置分类名称
        data.categName=layui.$("select[name='categId'] option:selected").text();
        data.trafficFee = data.trafficFee*100;
        data.hourFee = data.hourFee*100;
        data.checkFee = data.checkFee*100;
        // submitData(data);
        //一个分组下只能有一个默认服务项,已存在默认项目
        if(json.faultTariff.tariffType==1){
            data.priceSplit=1;
            data.partRatio='';
            data.artRatio='';
            data.guaranteeRatio='';
        }
        if(data.updateMyself=='false'&&data.hasDefaultItem=='true' && data.general=='2'){
            parent.layui.layer.confirm('该故障分组下已存在默认项，提交之后将替换原有默认项', function(index){
                submitData(data);
            });
        }else{
            submitData(data);
        }
        return false;
    });

    form.on('radio(priceStandard)', function (data) {
        let value = data.value;
        //是否区分明细
        let priceSplit = $('[name="priceSplit"]:checked').val();
        doRenderPriceDiv(value,priceSplit,json.faultTariff.tariffType);
        form.render(null, "zmn-form");
    });
    form.on('select(categId)', function (data) {
        let value = data.value;
        if (value == MAINTAIN_ITEM){
            $("#feeDetail3").show()
        }else{
            $("#feeDetail3").hide()
        }
    });

    form.on('radio(priceSplit)', function (data) {
        let value = data.value;
        let oldVal = json.item.priceSplit;
        //区分明细的情况下费用必填
        if (value == 2 && json.faultTariff.tariffType!=4){
            $("#feeDetail").show();
            $("#feeDetail3").hide();
            if(oldVal==value){
                $("#partRatio").val(json.item.partRatio);
                $("#artRatio").val(json.item.artRatio);
                $("#guaranteeRatio").val(json.item.guaranteeRatio);
                $("#checkFee").val(json.item.checkFee);
                $("#hourFee").val(json.item.hourFee);
                $("#trafficFee").val(json.item.trafficFee);
            }
            $("#feeDetail2 input").attr("lay-verify",'positive|percent');
            $("#partRatio").attr("lay-verify",'required|percent');
            $("#artRatio").attr("lay-verify",'required|percent');
            $("#guaranteeRatio").attr("lay-verify",'required|percent');
            $(".editFee").attr("lay-verify",'required|feeVerify');
        }else if (value == 2 && json.faultTariff.tariffType==4) {
            $("#feeDetail").hide();
            if($("#categId").val() == MAINTAIN_ITEM) {
                $("#feeDetail3").show();
            }
        }else {
            $("#feeDetail").hide();
            $("#feeDetail3").hide();
            $("#feeDetail2 input").attr("lay-verify", '');
            $("#partRatio").attr("lay-verify", '');
            $("#artRatio").attr("lay-verify", '');
            $("#guaranteeRatio").attr("lay-verify", '');
            $(".editFee").attr("lay-verify", '');
        }
        form.render(null, "zmn-form");
    });

    //选择明细一明细二不可填，选择明细二明细一不可填
    form.on('radio(priceSplitType)', function (data) {
        let value = data.value;
        //明细二
        if (value == 1){
            $("#partRatio").removeAttr("disabled").removeClass('layui-disabled').attr("lay-verify",'required|percent');
            $("#artRatio").removeAttr("disabled").removeClass('layui-disabled').attr("lay-verify",'required|percent');
            $("#guaranteeRatio").removeAttr("disabled").removeClass('layui-disabled').attr("lay-verify",'required|percent');
            $(".editFee").removeAttr("disabled").removeClass('layui-disabled').attr("lay-verify",'required|feeVerify');
            $("#feeDetail2 input").prop("disabled","disabled").addClass('layui-disabled').attr("lay-verify",'');
        }else{
            $("#feeDetail2 input").removeAttr("disabled").removeClass('layui-disabled').attr("lay-verify",'positive|percent');
            $("#factoryFee").prop("disabled","disabled").addClass('layui-disabled').attr("lay-verify",'');
            $("#partRatio").prop("disabled","disabled").addClass('layui-disabled').attr("lay-verify",'');
            $(".editFee").prop("disabled","disabled").addClass('layui-disabled').attr("lay-verify",'');
            $("#artRatio").prop("disabled","disabled").addClass('layui-disabled').attr("lay-verify",'');
            $("#guaranteeRatio").prop("disabled","disabled").addClass('layui-disabled').attr("lay-verify",'');
        }
        form.render(null, "zmn-form");
    });

    //工艺费 = 100-材料费
    $("#materialFee").blur(function () {
        let current = $(this).val();
        if(current){
            $("#factoryFee").val(100-current);
        }
    });


    //一口价区分明细初始化,区分联动还是初始化，联动需要show，初始化不需要
    function doRenderPriceDiv(priceStandard,priceSplit,tariffType,isInit) {
        //新增的情况下
        if(!priceStandard&&!priceSplit){
            $("#feeDetail2 input").attr("lay-verify",'');
            $("#partRatio").attr("lay-verify",'');
            $("#artRatio").attr("lay-verify",'');
            $(".editFee").attr("lay-verify",'');
            $("#guaranteeRatio").attr("lay-verify",'');
            if(tariffType==2){
                $("#feeDetail").show();
                $("#feeDetail3").hide();
                $("#priceSplitDiv").show();
                $("#feeDetail2 input").attr("lay-verify",'positive|percent');
                $("#partRatio").attr("lay-verify",'required|percent');
                $(".editFee").attr("lay-verify",'required|feeVerify');
                $("#artRatio").attr("lay-verify",'required|percent');
                $("#guaranteeRatio").attr("lay-verify",'required|percent');
            }
            if(tariffType==4){
                $("#feeDetail").hide();
                if($("#categId").val() == MAINTAIN_ITEM) {
                    $("#feeDetail3").show();
                }
            }
            $(":radio[name='manualPrice'][value='1']").prop("checked","checked");
            $(":radio[name='manualPrice']").prop("disabled","disabled");
            checkAndRanderCityLimit(isInit);
            form.render(null, "zmn-form");
            return;
        }
        //一口价
        if(priceStandard==2){
            //区分明细
            if(priceSplit==2 && tariffType != 4){
                $("#feeDetail").show();
                $("#feeDetail3").hide();
                $("#priceSplitDiv").show();
                $("#feeDetail2 input").attr("lay-verify",'required|percent');
                $("#partRatio").attr("lay-verify",'required|percent');
                $(".editFee").attr("lay-verify",'required|feeVerify');
                $("#artRatio").attr("lay-verify",'required|percent');
                $("#guaranteeRatio").attr("lay-verify",'required|percent');
            }else if(priceSplit==2 && tariffType == 4){
                $("#feeDetail").hide();
                if($("#categId").val() == MAINTAIN_ITEM) {
                    $("#feeDetail3").show();
                }
            }else{
                $("#feeDetail").hide();
                $("#feeDetail3").hide();
                $("#priceSplitDiv").show();
                $("#feeDetail2 input").attr("lay-verify",'');
                $("#partRatio").attr("lay-verify",'');
                $(".editFee").attr("lay-verify",'');
                $("#artRatio").attr("lay-verify",'');
                $("#guaranteeRatio").attr("lay-verify",'');
            }
            //修改价格整项置灰不可选择
            $(":radio[name='manualPrice']").prop("disabled","disabled");
            $(":radio[name='manualPrice'][value='1']").prop("checked","checked");
            //非默认
            $(":radio[name='general']").removeAttr("disabled");
            //城市系数
            $(":radio[name='cityLimit']").removeAttr("disabled");
            //品牌系数
            $(":radio[name='brandLimit']").removeAttr("disabled");

            //非初始化需要联动
            if(!isInit){
                $(":radio[name='general']").removeAttr("checked");
                $(":radio[name='cityLimit']").removeAttr("checked");
                $(":radio[name='brandLimit']").removeAttr("checked");
            }
            //明细类型
            let priceSplitTypeVal = $(":radio[name='priceSplitType']:checked").val();
            if(priceSplitTypeVal==1){
                $("#feeDetail2 input").prop("disabled","disabled").addClass("layui-disabled").attr("lay-verify",'');
                $("#partRatio").attr("lay-verify",'required|percent');
                $(".editFee").attr("lay-verify",'required|feeVerify');
                $("#artRatio").attr("lay-verify",'required|percent');
                $("#guaranteeRatio").attr("lay-verify",'required|percent');
            }
            else if(priceSplitTypeVal==2){
                $("#partRatio").prop("disabled","disabled").addClass("layui-disabled").attr("lay-verify",'');
                $("#artRatio").prop("disabled","disabled").addClass("layui-disabled").attr("lay-verify",'');
                $("#guaranteeRatio").prop("disabled","disabled").addClass("layui-disabled").attr("lay-verify",'');
                $("#feeDetail2 input").attr("lay-verify",'positive|percent')
                $(".editFee").prop("disabled","disabled").addClass("layui-disabled").attr("lay-verify",'');
            }
            form.render(null, "zmn-form");
        }else{
            $("#feeDetail").hide();
            $("#feeDetail3").hide();
            $("#priceSplitDiv").hide();
            //非默认项，城市系数、品牌系数必须为不影响
            $(":radio[name='manualPrice']").removeAttr("disabled");

            //非默认
            $(":radio[name='general']").prop("disabled","disabled");
            $(":radio[name='general'][value='1']").prop("checked","checked");
            //城市系数
            $(":radio[name='cityLimit']").prop("disabled","disabled");
            $(":radio[name='cityLimit'][value='1']").prop("checked","checked");
            //品牌系数
            $(":radio[name='brandLimit']").prop("disabled","disabled");
            $(":radio[name='brandLimit'][value='1']").prop("checked","checked");
            if(!isInit){
                $(":radio[name='manualPrice']").removeAttr("checked");
            }
            $("#feeDetail2 input").attr("lay-verify",'');
            $("#partRatio").attr("lay-verify",'');
            $(".editFee").attr("lay-verify",'');
            $("#artRatio").attr("lay-verify",'');
            $("#guaranteeRatio").attr("lay-verify",'');
            form.render(null, "zmn-form");
        }
        //针对服务价格
        if(tariffType==1){
            $("#feeDetail").hide();
            $("#feeDetail3").hide();
            $("#priceSplitDiv").hide();
        }
        checkAndRanderCityLimit(isInit);
        // 城市定价，城市系数影响默认不影响，不允许修改
        form.render(null, "zmn-form");
    }

    function checkAndRanderCityLimit(isInit){
        let type = json.faultTariff.priceType;
        if(type==2){
            if(isInit){
                $(":radio[name='cityLimit'][value='1']").prop("checked","checked");
            }
            // $(":radio[name='cityLimit']").prop("disabled","disabled");
        }
    }

    function submitData(data){

        let desc = data.desc;
        if (!zmnUtil.isEmptyNull(desc) && desc.length > 2000) {
            parent.layui.layer.msg("项目说明长度不能超过2000个字符", {"icon": 2, time: 1000});
            return false;
        }
        //
        // let acceptProjectList = xmSelect.get("#acceptProjectList",true).getValue();
        // let serviceContentList = xmSelect.get("#serviceContentList",true).getValue();
        // let faultList = xmSelect.get("#faultList",true).getValue();
        // data.acceptProjectList = [];
        // data.serviceContentList = [];
        // data.faultList = [];
        // if(acceptProjectList != null && acceptProjectList.length > 0){
        //     for (var i = 0;i<acceptProjectList.length;i++) {
        //         data.acceptProjectList.push({name: acceptProjectList[i].name, value: acceptProjectList[i].value})
        //     }
        // }
        // if(serviceContentList != null && serviceContentList.length > 0){
        //     for (var i = 0;i<serviceContentList.length;i++) {
        //         data.serviceContentList.push({name: serviceContentList[i].name, value: serviceContentList[i].value})
        //     }
        // }
        // if(faultList != null && faultList.length > 0){
        //     for (var i = 0;i<faultList.length;i++) {
        //         data.faultList.push({name: faultList[i].name, value: faultList[i].value})
        //     }
        // }
        $.ajax({
            async: false,
            type: "POST",
            url: "/service/item/addEdit.action",
            dataType: "json",
            contentType: "application/json",
            data: JSON.stringify(data),
            success: function (rs) {
                if (rs.status === 200) {
                    parent.layui.layer.msg(rs.message,{time: 1000},()=>{
                        cancel();
                        return true;
                    });

                }else {
                    parent.layui.layer.msg(rs.message,{"icon":2,area:'750px'});
                    return false;
                }
            }
        });
    }


    //对外暴露的接口
    exports('serv_service_item_detail', {});
});
function cancel() {
    window.location.href="/service/group/list.action?servCategId="+json.group.servCategId+"&categOneId="+json.group.categOneId+"&tariffId="+json.group.tariffId;
}